<template>
  <el-container class="home-container">
    <!-- 页面头部 -->
    <el-header>
      <div class="logo">
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut" class="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <el-aside :width="toggleTemp ? '64px' : '200px'">
        <div class="toggle-menu" @click="toggleMenu">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="toggleTemp"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <el-submenu :index="item.id + ''" v-for="item in MenuList" :key="item.id">
            <template slot="title">
              <i :class="iconList[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index=" '/' +itemChild.path" @click="getActivePath( '/' +itemChild.path)" v-for="itemChild in item.children" :key="itemChild.id">
              <i class="el-icon-menu"></i>
              <span slot="title">{{itemChild.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  name: 'Home',
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('menuItemPath')
  },
  data () {
    return {
      MenuList: [],
      iconList: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      toggleTemp: false,
      activePath: ''
    }
  },
  methods: {
    // 退出登录功能
    loginOut () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 利用axios获取nav的数据
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.MenuList = res.data
      console.log(res)
    },
    // 设置是否隐藏导航
    toggleMenu () {
      this.toggleTemp = !this.toggleTemp
    },
    // 设置导航是否高亮显示 将path存放在window.sessionStorage中  这样页面刷新也可以保持高亮状态
    getActivePath (activePath) {
      window.sessionStorage.setItem('menuItemPath', activePath)
      this.activePath = activePath
      console.log(this.activePath)
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  align-items: center;
  .logo {
    display: flex;
    align-items: center;
    span {
      margin-left: 20px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: 0;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-menu {
  background-color: lightblue;
  color: #fff;
  font-size: 16px;
  line-height: 26px;
  text-align: center;
  letter-spacing: 0.2em;
}
</style>
